﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.DynamicApi;
using ZhonTai.Industry.Domain.PLCDevice;
using ZhonTai.Admin.Services;
using ZhonTai.Industry.Services.PLCDevice.Dto;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Industry.Services.WorkStation.Dto.Input;
using ZhonTai.Industry.Services.PLCDevice.Dto.Output;
using ZhonTai.Industry.Services.PLCDevice.Dto.Input;

namespace ZhonTai.Industry.Services.PLCDevice
{
    /// <summary>
    /// PLC设备配置服务
    /// </summary>
    [DynamicApi(Area = IndustryConsts.AreaName)]
    public class PLCDeviceService : BaseService, IPLCDeviceService, IDynamicApi
    {
        private IPLCDeviceRepository _pLCDeviceRepository => LazyGetRequiredService<IPLCDeviceRepository>();

        public PLCDeviceService()
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PLCDeviceGetOutput> GetAsync(long id)
        {
            var output = await _pLCDeviceRepository.GetAsync<PLCDeviceGetOutput>(id);
            return output;
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IEnumerable<PLCDeviceGetListOutput>> GetListAsync(PLCDeviceGetListInput input)
        {
            var list = await _pLCDeviceRepository.Select
                .OrderByDescending(a => a.Id)
                .ToListAsync<PLCDeviceGetListOutput>();
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<PLCDeviceGetPageOutput>> GetPageAsync(PageInput<PLCDeviceGetPageInput> input)
        {
            var filter = input.Filter;
            var select = _pLCDeviceRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                .Count(out var total);
            if (input.SortList != null && input.SortList.Count > 0)
            {
                input.SortList.ForEach(sort =>
                {
                    select = select.OrderByPropertyNameIf(sort.Order.HasValue, sort.PropName, sort.IsAscending.Value);
                });
            }
            else
            {
                select = select.OrderBy(a => a.Sort);
            }
            var list = await select.OrderByDescending(c => c.Id)
              .Page(input.CurrentPage, input.PageSize)
              .ToListAsync<PLCDeviceGetPageOutput>();
            //关联查询代码
            var data = new PageOutput<PLCDeviceGetPageOutput> { List = list, Total = total };

            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<long> AddAsync(PLCDeviceAddInput input)
        {
            var entity = Mapper.Map<PLCDeviceEntity>(input);
            var id = (await _pLCDeviceRepository.InsertAsync(entity)).Id;

            return id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateAsync(PLCDeviceUpdateInput input)
        {
            var entity = await _pLCDeviceRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("PLC设备配置不存在！");
            }

            Mapper.Map(input, entity);
            await _pLCDeviceRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(long id)
        {
            return await _pLCDeviceRepository.DeleteAsync(id) > 0;
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> SoftDeleteAsync(long id)
        {
            return await _pLCDeviceRepository.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> BatchSoftDeleteAsync(long[] ids)
        {
            return await _pLCDeviceRepository.SoftDeleteAsync(ids);
        }

        /// <summary>
        /// 设置启用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task SetEnableAsync(PLCDeviceSetEnableInput input)
        {
            var entity = await _pLCDeviceRepository.GetAsync(input.PLCDeviceId);
            entity.Enabled = input.Enabled;
            await _pLCDeviceRepository.UpdateAsync(entity);
        }
    }
}